package com.buddy.learn.orm.demo;

import com.buddy.learn.orm.demo.entity.Member;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class JdbcDemo {

    public static void main(String[] args) {
        List<?> result = select();
    }

    private static List<?> select() {
        List<Member> resultList = new ArrayList<>();
        Connection con = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try {
            //1.加载驱动类
            Class.forName("com.mysql.jdbc.Driver");
            //2.建立连接
            con = DriverManager.getConnection("", "", "");
            //3.创建语句集
            String sql = "SELECT * FROM t_member";
            pstm = con.prepareStatement(sql);
            //4.执行语句集
            rs = pstm.executeQuery();
            //5.获取结果集
            while (rs.next()) {
                Member member = new Member();
                member.setId(rs.getLong("id"));
                member.setName(rs.getString("name"));
                member.setAddr(rs.getString("addr"));
                member.setAge(rs.getInt("age"));
                resultList.add(member);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //6.关闭结果集、语句集、关闭连接
            try {
                rs.close();
                pstm.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return resultList;
    }
}
